package com.todo.event.activity.iface.backend.common;


import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.StringUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/1/11 12:31
 * @description
 */
@Data
@Schema(description = "活动执行信息")
public class ActivityExecuteInfo {

    @Schema(title = "流量占比", description = "流量占比")
    private Integer FlowRatio;

    @Schema(title = "分流成名称", description = "分流成名称")
    private String flowName;

    @Schema(title = "分流code", description = "分流code，后续用于记录策略命中的分流")
    private String flowCode;

    @Schema(title = "是否对照组", description = "是否对照组")
    private Boolean isCompared;

    @Schema(title = "奖励发放列表", description = "奖励发放列表")
    private List<PropertyReward> propertyRewardList;

    @Schema(title = "消息触达", description = "消息触达")
    private MessageTouch messageTouch;

    @Data
    public static class PropertyReward {

        @Schema(title = "奖励分类", description = "奖励分类")
        private Integer rewardType;

        @Schema(title = "发放Id", description = "发放Id")
        private String grantId;
    }

    @Data
    public static class MessageTouch {

        @Schema(title = "执行类型", description = "执行类型")
        private Integer executeClassify;

        @Schema(title = "关联的业务Id", description = "关联的业务Id，如短信为模板id，供下游执行")
        private String relationId;

        @Schema(title = "关联业务数据", description = "关联业务数据")
        private String relationData;

        @Schema(title = "是否延迟执行", description = "是否延迟执行")
        private Boolean isInterval;

        @Schema(title = "间隔时间", description = "间隔时间 分钟")
        private Integer intervalTime;

        @Schema(title = "是否动态获取信息", description = "是否动态获取信息")
        private Boolean isDynamicInfo;

        @Schema(title = "动态词汇", description = "动态词汇，用于常见的并且消息中不携带的信息，当前系统动态获取的一些数据，支持业务扩展，并枚举维护")
        private List<String> dynamicWords;
    }

    public boolean baseCheck() {
        if (FlowRatio == null || FlowRatio > 100 || FlowRatio <= 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR, "流量占比必须大于0且小于100");
        }
        if (StringUtils.isBlank(flowName)) {
            return false;
        }
        if (CollectionUtils.isNotEmpty(propertyRewardList)) {
            for (PropertyReward propertyReward : propertyRewardList) {
                if (propertyReward.rewardType == null || StringUtils.isBlank(propertyReward.grantId)) {
                    return false;
                }
            }
        }
        if (messageTouch != null) {
            if (messageTouch.relationId == null) {
                return false;
            }
            if (messageTouch.isDynamicInfo && CollectionUtils.isEmpty(messageTouch.dynamicWords)) {
                return false;
            }
            if (messageTouch.isInterval && messageTouch.intervalTime == null) {
                return false;
            }
        }
        return true;
    }
}
